# routers/shop.py
# 商家主页接口：分页、排序、展示在售卡牌
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from models import User, UserCard, Card
from schemas import UserCardOut
from deps import get_db
from typing import List

router = APIRouter(prefix="/shop", tags=["商家"])

@router.get("/{seller_id}/cards", response_model=List[UserCardOut])
def get_seller_cards(seller_id: int, skip: int = 0, limit: int = 20, sort_by: str = "price", order: str = "asc", db: Session = Depends(get_db)):
    """获取商家在售卡牌，支持分页和排序"""
    query = db.query(UserCard).filter(UserCard.owner_id == seller_id)
    if sort_by == "price":
        query = query.order_by(UserCard.price.asc() if order == "asc" else UserCard.price.desc())
    elif sort_by == "name":
        query = query.join(Card).order_by(Card.name.asc() if order == "asc" else Card.name.desc())
    elif sort_by == "quantity":
        query = query.order_by(UserCard.quantity.asc() if order == "asc" else UserCard.quantity.desc())
    return query.offset(skip).limit(limit).all() 